package de.hska.master.algo;

import de.hska.master.algo.mergeHull.MergeHullAlgorithm;
import de.hska.master.algo.quickHull.QuickHullAlgorithm;
import de.hska.master.model.Model;

public class TimeMeasurement {

	private Model model = null;
	private long timeBefore = 0;
	private long duration = 0;

	/**
	 * Creates a instance of timemeasurement to execute a graham algo run on
	 * model and show it on viewer
	 * 
	 * @param model
	 *            where graham scan works on
	 * @param viewer
	 *            on which the convex hull is shown
	 */
	public TimeMeasurement(Model model) {
		this.model = model;
	}

	/**
	 * Starts the time measuring, executes the graham algo and ends the time
	 * measuring. Duration of execution is stored in member duration.
	 * 
	 * @throws InterruptedException
	 */
	public void execAndMeasureAlgorithmGrahamScan() throws InterruptedException {
		beginMeasure();
		execAlgorithmGrahamScan();
		endMeasure();
	}

	/**
	 * Starts the time measuring, executes the merge hull algo and ends the time
	 * measuring. Duration of execution is stored in member duration.
	 * 
	 * @throws InterruptedException
	 */
	public void execAndMeasureAlgorithmMergeHull() throws InterruptedException {
		beginMeasure();
		execAlgorithmMergeHull();
		endMeasure();
	}
	
	/**
	 * Starts the time measuring, executes the quick hull algo and ends the time
	 * measuring. Duration of execution is stored in member duration.
	 * 
	 * @throws InterruptedException
	 */
	public void execAndMeasureAlgorithmQuickHull() throws InterruptedException {
		beginMeasure();
		execAlgorithmQuickHull();
		endMeasure();
	}

	private void beginMeasure() {
		timeBefore = System.currentTimeMillis();
	}

	/**
	 * before: beginMeasure
	 */
	private void execAlgorithmMergeHull() throws InterruptedException {
		MergeHullAlgorithm algo = new MergeHullAlgorithm(model);
		algo.runAlgorithm();
	}
	
	/**
	 * before: beginMeasusure
	 * @throws InterruptedException 
	 */
	private void execAlgorithmQuickHull() throws InterruptedException {
		QuickHullAlgorithm algo = new QuickHullAlgorithm(model);
		algo.runAlgorithm();
	}

	/**
	 * before: beginMeasure
	 */
	private void execAlgorithmGrahamScan() throws InterruptedException {
		GrahamAlgorithm algo = new GrahamAlgorithm(model);
		algo.runAlgorithm();
	}

	/**
	 * before: execAlgorithm
	 */
	private void endMeasure() {
		long timeAfter = System.currentTimeMillis();
		duration = timeAfter - timeBefore;
	}

	/**
	 * Member execAndMeasureAlgorithm() must be called before. This method
	 * returns the duration of the algo in ms
	 * 
	 * @return duration of algo run
	 */
	public long getDuration() {
		return duration;
	}
}
